//
//  warningViewController.m
//  WHB_driver
//
//  Created by 徐国庆 on 15/12/8.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "warningViewController.h"
#import "warningDetailViewController.h"
#import "warningCell.h"
#import "User.h"

@interface warningViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(strong, nonatomic)    UICollectionView *collectionView;
@property(nonatomic, strong)NSArray *array;
@property(nonatomic, strong)NSArray *imageNameArray;
@property(nonatomic, strong)NSArray *pressedArray;//*btn_icon_Alarm_overspeed_pressed;
@property(nonatomic, strong)User *user;

@end

@implementation warningViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSArray arrayWithObjects:@"事故报警", @"超速报警", @"疲劳报警", @"怠速报警", @"亏电报警", @"区域报警", @"离线报警", nil];
        self.imageNameArray = [NSArray arrayWithObjects:@"btn_icon_Alarm_accident_normal", @"btn_icon_Alarm_overspeed_normal", @"btn_icon_Alarm_fatigue_normal", @"btn_icon_Alarm_lowspeed_normal",   @"btn_icon_Alarm_electric_normal",  @"btn_icon_Alarm_area_normal", @"btn_icon_Alarm_offline_normal",   nil];
        self.pressedArray = [NSArray arrayWithObjects:@"btn_icon_Alarm_accident_pressed", @"btn_icon_Alarm_overspeed_pressed", @"btn_icon_Alarm_fatigue_pressed", @"btn_icon_Alarm_lowspeed_pressed",   @"btn_icon_Alarm_electric_pressed",  @"btn_icon_Alarm_area_pressed", @"btn_icon_Alarm_offline_pressed", nil];
        //取得用户信息
        self.user = (User *)[NSKeyedUnarchiver unarchiveObjectWithFile:kUserPath];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navtitle.text = @"告警";
    self.leftButton.hidden = YES;
    [self initWithView];
    [self getDriverAlarmTimes];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"warningCell";
    warningCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifer forIndexPath:indexPath];
    cell.backgroundColor = [UIColor HexStrWithColor:@"eeeeee"];
    if (indexPath.row % 2 == 0) {
//        cell.layer.borderColor = [UIColor HexStrWithColor:@"999999"].CGColor;
    }
    cell.bottomView.layer.borderColor = [UIColor HexStrWithColor:@"999999"].CGColor;
    cell.warningImageView.image = [UIImage imageNamed:[self.imageNameArray objectAtIndex:indexPath.row]];
    cell.warningLabel.text = [self.array objectAtIndex:indexPath.row];
    cell.infoImageView.image = [UIImage imageNamed:@"BG_RedDot"];
    cell.infoLabel.text = @"10";
    
    return cell;
}

#pragma mark 定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = kUIScreenWidth/2;
    CGFloat height = width;
    return CGSizeMake(width, height);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    warningDetailViewController *detailVC = [[warningDetailViewController alloc] initWithTitle:[self.array objectAtIndex:indexPath.row]];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark 初始化视图 创建button
- (void)initWithView{
    
    //列表
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 布局设置都在这里
    flowLayout.itemSize = CGSizeMake(kUIScreenWidth / 2 , kUIScreenWidth / 2 );
    // 最小行间距
    flowLayout.minimumLineSpacing = 0;
    // 最小item的间距
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - kNavHeight - 5) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.minimumZoomScale = 0;
    self.collectionView.maximumZoomScale = 0;
    self.collectionView.backgroundColor = [UIColor HexStrWithColor:@"f8f3ed"];
    self.collectionView.showsVerticalScrollIndicator = NO;//竖向滚动条是否显示
    [self.view addSubview:self.collectionView];
    // 注册重用标志
    [self.collectionView registerClass:[warningCell class] forCellWithReuseIdentifier:@"warningCell"];
}

// 推出设置页
-(void)perFormAdd:(UIButton *)button{
    NSLog(@"设置");
}

- (void)getDriverAlarmTimes{
    [DataController getDriverAlarmTimes:[NSNumber numberWithLong:self.user.userId]
                                 userId:[NSNumber numberWithLong:self.user.userId]
                                   imei:kIdentifierString
                              withBlock:^(NSDictionary *result) {
                                  NSLog(@"result ==>><<%@", result);
                              }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
